﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Entity;
using BLL;
namespace web.admin.scTask
{
    public partial class InnserPro : System.Web.UI.Page
    {
        private AcademyService academyService = new AcademyService();
        private ContestService contestService = new ContestService();
        private ModuleService moduleService = new ModuleService();
        private TeacherService teacherService = new TeacherService();
        private RoleService roleService = new RoleService();
        const int pagesize = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (HttpContext.Current.User != null &&
                HttpContext.Current.User.Identity.IsAuthenticated &&
                HttpContext.Current.User is SitePrincipal)
                {
                    SitePrincipal user = HttpContext.Current.User as SitePrincipal;
                    SiteIdentity si = user.Identity as SiteIdentity;
                    if (si.UserType != 0)
                        return ;
                    BoTeachers teacher = teacherService.GetTeacher(si.UserID);
                    //BoRoles role = roleService.getRole(teacher.TEA_MANAGER);
                }
                
                RP_Bind();
                AcademyList.DataSource = academyService.getAllAcademies();
                AcademyList.DataTextField = "ACA_NAME";
                AcademyList.DataValueField = "ACA_ID";
                AcademyList.DataBind();
                if (Session["selectValue"] != null && Session["selectValue"].ToString() != "")
                    AcademyList.SelectedValue = Session["selectValue"].ToString();
            }
        }

       
        /// <summary>
        /// reapeater控件绑定
        /// </summary>
        protected void RP_Bind()
        {
            Pager1.PageSize = pagesize;
            int records = 0; 
            string permissionStr = string.Empty;
            if (Session["AdminPermission"] != null && Session["AdminPermission"].ToString() != "")
                permissionStr = new DbCommon.DbHelper().GetSingleItem("UserClass", "UserPermission", "UserId=" + Session["AdminPermission"].ToString()).ToString();
            
            /*
            if (Session["innerCon"] != null && Session["innerCon"].ToString()!="")
                RpList.DataSource=contestService.GetPagerInnerContests(Session["innerCon"].ToString(), " CON_ID DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            else
                RpList.DataSource = contestService.GetPagerInnerContests("", " CON_ID DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            */
            if (HttpContext.Current.User != null &&
                HttpContext.Current.User.Identity.IsAuthenticated &&
                HttpContext.Current.User is SitePrincipal)
            {
                SitePrincipal user = HttpContext.Current.User as SitePrincipal;
                SiteIdentity si = user.Identity as SiteIdentity;
                if (si.UserType != 0)
                    return;

                BoTeachers teacher = teacherService.GetTeacher(si.UserID);
                if (teacher.TEA_MANAGER == 0)
                {
                    if (Request["PageCode"] == "Enroll")
                    RpList.DataSource = contestService.GetPagerInnerContestsByAcademy2(AcademyList.SelectedValue, Pager1.PageSize, Pager1.PageIndex, out records);
                    else 
                        RpList.DataSource = contestService.GetPagerInnerContestsByAcademy(AcademyList.SelectedValue, Pager1.PageSize, Pager1.PageIndex, out records);
                }
                else
                {
                    if (Request["PageCode"] == "Enroll")
                    RpList.DataSource = contestService.GetPagerInnerContestsByTeacher2(teacher.TEA_ID, Pager1.PageSize, Pager1.PageIndex, out records);
                    else
                    RpList.DataSource = contestService.GetPagerInnerContestsByTeacher(teacher.TEA_ID, Pager1.PageSize, Pager1.PageIndex, out records);
                }
            }
            RpList.DataSource = contestService.GetPagerInnerContests(" COM_TYPE=0", " CON_ID DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            RpList.DataBind();
            //初始化分页条,设置样式
            Pager1.SetPage(records);    
            Pager1.ShowPageJump = true;
            Pager1.ShowPageLan = 0; //导航条语言:0=默认，1=中文，2=英语
          
        }
        protected void AcademyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selAcamediyId = AcademyList.SelectedValue;
            IList<BoContests> comps = new List<BoContests>();
            if (selAcamediyId == "")
            {
                Session["selectValue"] = "";
                Session["innerCon"] = "";
            }
            else
            {
                Session["selectValue"] = selAcamediyId;
                Session["innerCon"] = "ACA_ID='" + selAcamediyId + "'";
            }
            RP_Bind();
        }
        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("Hf_id")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    BoContests innerpao = contestService.getContest(id);
                    contestService.DeleteContest(innerpao);
                    j++;
                }
            }
            switch (Request .QueryString["PageCode"])
            {case "Task":Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='InnerPro.aspx?PageCode=Task'; </script>");
                    break ;
                case "Enroll":Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='InnerPro.aspx?PageCode=Enroll'; </script>");
                    break ;
                    case "stusummary": Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='InnerPro.aspx?PageCode=stusummary'; </script>");
                      break;
                    case "Money": Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='InnerPro.aspx?PageCode=Money'; </script>");
                      break;

            }
            
            
           
        }

        protected void RpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                BoContests contest = (BoContests)e.Item.DataItem;
                HtmlTableCell cell = (HtmlTableCell)e.Item.FindControl("Operation");
                String s = Request["PageCode"];
                String pageName = "ListInnerProc.aspx";
                String opeName = "过程管理";
                String parameter = "";
                bool showToolbar = false;

                if (String.IsNullOrEmpty(s) || s == "Procedure")
                {
                    pageName = "ListInnerProc.aspx";
                    opeName = "过程管理";
                }
                else if (s == "Buget")
                {
                    pageName = "InnerBuget.aspx";
                    opeName = "预算管理";
                }
                else if (s == "Audit")
                {
                    pageName = "InnerCheck.aspx";
                    opeName = "审核管理";
                }
                else if (s == "Enroll")
                {
                    pageName = "InnerPar.aspx";
                    opeName = "报名管理";
                    
                    showToolbar = true;

                    cell.InnerHtml = "<a href='" + pageName + "?id=" + contest.CON_ID +"'>" + opeName + "</a> ";

                    toos_box.Visible = true;
                    return;
                }
                else if (s == "Picture")
                {
                    pageName = "InnerPictures.aspx";
                    opeName = "图片管理";
                }
                else if (s == "Award")
                {
                    pageName = "InnerAwards.aspx";
                    opeName = "获奖管理";
                }
                else if (s == "Work") 
                {
                    pageName = "InnerWorks.aspx";
                    opeName = "作品管理";
                }
                else if (s == "Summary")
                {
                    pageName = "InnerSummary.aspx";
                    opeName = "总结管理";
                }
                    else if(s=="stusummary")
                {
                    pageName = "StuSummary.aspx";
                    opeName = "获奖感言";
                    //parameter = "&OPERATION=Modify";
                    showToolbar = true;

                    cell.InnerHtml = "<a href='" + pageName + "?id=" + contest.CON_ID + "'>" + opeName + "</a> ";
                    //cell.InnerHtml += "<a href='InnerMoney.aspx?id=" + contest.CON_ID + "'>费用管理</a>";
                    toos_box.Visible = true;
                    return;
                }
                else if (s == "Money")
                {
                    pageName = "InnerMoney.aspx";
                    opeName = "费用管理";
                    parameter = "&OPERATION=Modify";
                    showToolbar = true;

                    cell.InnerHtml = "<a href='" + pageName + "?id=" + contest.CON_ID + parameter + "'>" + opeName + "</a> ";
                    //cell.InnerHtml += "<a href='InnerMoney.aspx?id=" + contest.CON_ID + "'>费用管理</a>";
                    toos_box.Visible = true;
                    return;
                }
                else if (s == "Task")
                {
                    pageName = "AddInnerTask.aspx";
                    opeName = "修改任务";
                    parameter = "&OPERATION=Modify";
                    showToolbar = true;

                    cell.InnerHtml = "<a href='" + pageName + "?id=" + contest.CON_ID + parameter + "'>" + opeName + "</a> ";
                    cell.InnerHtml += "<a href='InnerBuget.aspx?id=" + contest.CON_ID + "'>竞赛预算</a>";
                    toos_box.Visible = true;
                    return;
                }
                if (showToolbar)
                    toos_box.Visible = true;
                else
                    toos_box.Visible = false;
                cell.InnerHtml = "<a href='" + pageName + "?id=" + contest.CON_ID + parameter + "'>" + opeName + "</a>";
            }

        }
    }
}